`
https://leetcode.cn/problems/longest-common-subsequence/
`

/**
 * @param {string} text1
 * @param {string} text2
 * @return {number}
 */
var longestCommonSubsequence = function (text1, text2) {
  const m = text1.length, n = text2.length

  // dp[i][j] 表示 text1[0...i-1] 和 text2[0...j-1] 的 lcs
  const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0))

  for (let i = 1; i <= m; i++) {
    for (let j = 1; j <= n; j++) {
      // 当前字符匹配，lcs 长度 + 1
      if (text1[i - 1] === text2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1
      } else {
        // text1[i] 或 text2[j] 不在 lcs 内
        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1])
      }
    }
  }

  return dp[m][n]
};

// test
const text1 = "abcde"
const text2 = "ace"
console.log(longestCommonSubsequence(text1, text2)) // 3